//
//  NSMCSSearchAssociateController.m
//  BSProject
//
//  Created by nsm on 2020/4/13.
//  Copyright © 2020 beisheng. All rights reserved.
//

#import "NSMCSSearchAssociateController.h"

@interface NSMCSAssociateCell : NSMTableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation NSMCSAssociateCell
- (void)setObject:(CityModel *)obj {
    _titleLabel.text = obj.name;
}

- (void)setupSubviews {
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    layout.backgroundColor = UIColor.whiteColor;
    layout.myHorzMargin = 0;
    layout.cacheEstimatedRect = YES;
    layout.padding = UIEdgeInsetsMake(15, 15, 15, 15);
    layout.subviewVSpace = 4;
    layout.gravity = MyGravity_Horz_Fill;
    [self.contentView addSubview:layout];
    self.rootLayout = layout;
    
    UILabel *label = [UILabel new];
    label.wrapContentHeight = YES;
    [layout addSubview:label];
    _titleLabel = label;
}
@end



@interface NSMCSSearchAssociateController ()

@end

@implementation NSMCSSearchAssociateController
@synthesize associateURL = _associateURL ,didSelectAssociateItem = _didSelectAssociateItem;

#pragma mark - lifeCycle

#pragma mark - override

#pragma mark - initialization
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sourceManager.classForCellAtIndexPath = ^Class(NSIndexPath *indexPath) {
        return NSMCSAssociateCell.class;
    };
}

#pragma mark - setter/getter

#pragma mark - network

#pragma mark - NSMSearchAssociateProtocol
- (void)associateWithKeywords:(NSString *)keywords {
    // 去首尾空格
    NSString *searchText = [keywords stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    // 根据中间空格分割
    NSArray *searchItems = [searchText componentsSeparatedByString:@" "];
    NSMutableArray *andPredicates = [NSMutableArray new];
    for (NSString *item in searchItems) {
        NSMutableArray *subPredicates = [NSMutableArray new];
        NSExpression *lep = [NSExpression expressionForKeyPath:@"name"];
        NSExpression *rep = [NSExpression expressionForConstantValue:item];
        NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:lep rightExpression:rep modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption];
        [subPredicates addObject:predicate];
        
        NSCompoundPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
        [andPredicates addObject:orPredicate];
    }
    
    NSCompoundPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:andPredicates];
    
    NSArray *results = [self.searchs filteredArrayUsingPredicate:finalPredicate];
    self.tableView.sourceManager.dataArray = SafeArray(results).mutableCopy;
    [self.tableView reloadData];
}

#pragma mark - method/public

#pragma mark - method/private

#pragma mark - action

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
